#include<bits/stdc++.h>
using namespace std;
#define int long long


int s[1005];


void init(int n) {
	for (int i = 1; i <= n; i++)
		s[i] = i;
}


int find(int x) {
	if (s[x] != x)	s[x] = find(s[x]);
	return s[x];
}


void merge(int x, int y) {
	x = find(x);
	y = find(y);
	s[x] = s[y];
}


int count(int n) {
	int ans = 0;
	for (int i = 1; i <= n; i++)
		if (s[i] == i)
			ans++;
	return ans - 1;
}


signed main() {
	int n, m;

	while (cin >> n) {
		if (!n)	break;
		cin >> m;

		init(n);

		for (int i = 1; i <= m; i++) {
			int x, y;
			cin >> x >> y;
			merge(x, y);
		}

		cout << count(n) << endl;
	}


	return 0;
}
